//
//  DiscountedViewController.m
//  ProjectForA
//
//  Created by hmengwei on 14/11/19.
//  Copyright (c) 2014年 hmengwei. All rights reserved.
//

#import "DiscountedViewController.h"
#import "DiscountedView.h"
#import "DiscountedDetailsViewController.h"
#import "AsyncConnection.h"
#import "DiscountedModel.h"
#import "UIImageView+WebCache.h"
#import "DIscountedTableViewCell.h"
#import "MJRefresh.h"
@interface DiscountedViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation DiscountedViewController

-(void)dealloc
{
    [_myData release];
    [_disArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNila
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNila];
    if (self) {
        self.myData = [NSData data];
        self.disArray = [NSMutableArray array];
        self.butArray = [NSMutableArray arrayWithObjects:@"全部", @"自由行", @"达人带队", @"周边游", @"当地游",  nil];
        self.butArray1 = [NSMutableArray arrayWithObjects:@"全部", @"国内", @"东南亚", @"日韩", @"港澳台", @"欧洲", @"南亚", @"大洋洲", @"美洲", @"中东亚",  nil];
        self.butArray2 = [NSMutableArray arrayWithObjects:@"全部", @"北京", @"上海", @"广州", @"深圳", @"成都", @"南京", @"杭州", @"重庆", nil];
        self.butArray3 = [NSMutableArray arrayWithObjects:@"全部", @"11月", @"12月", @"2015年01月", @"02月", @"03月", @"04月", @"05月", nil];
        
        self.page = 2;
        
    }
    return self;
}


- (void)viewDidLoad {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"特价";
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor cyanColor]];
    [self connect];
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)connect
{//http://breadtrip.com/tp/products/?category=&dest=&start=&depart=&page=4
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"" forKey:@"category"];
    [dic setObject:@"" forKey:@"dest"];
    [dic setObject:@"" forKey:@"start"];
    [dic setObject:@"" forKey:@"depart"];
    [dic setObject:@"1" forKey:@"page"];
    [AsyncConnection getConnectWithUrl:Detail value:dic block:^(NSData *data) {
        _myData = data;

        [self createJson];
        [self createview];
        [self createTable];
        [self setupRefresh];
        [self createButtonTable];
        [self createButtonTable1];
        [self createButtonTable2];
        [self createButtonTable3];
//        [self createScroll];
    }];
}


-(void)createJson
{
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:_myData options:NSJSONReadingMutableContainers error:nil];
    NSArray *arr = [dic objectForKey:@"products"];
    for (NSDictionary *dice in arr) {
        DiscountedModel *disModel = [[DiscountedModel alloc] init];
        [disModel setValuesForKeysWithDictionary:dice];
        [_disArray addObject:disModel];
    }
    
    
}

//在导航栏下方添加一个self。with的高度为40的view，然后在上面添加四个button
-(void)createview
{
    //在self。view上是一个scrollview
    self.scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height )];
    [_scroller setContentSize:CGSizeMake(0, 0)];
    
    UIView *aview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    [aview setBackgroundColor:[UIColor whiteColor]];
    
    
    //设置四个button
    self.button1 = [[UIButton alloc] initWithFrame:CGRectMake(10 , 10, (aview.frame.size.width - 35) / 4, 30)];
    _button1.tag = 10;
    [_button1 setTitle:@"产品类型" forState:UIControlStateNormal];
    [_button1 setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    [_button1.titleLabel setFont:[UIFont systemFontOfSize:13]];
    _button1.layer.cornerRadius =10;
    _button1.layer.masksToBounds = YES;
    [_button1.layer setBorderWidth:1.0];
    [_button1.layer setBorderColor:[UIColor cyanColor].CGColor];//设置button边框颜色
    [_button1 addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [aview addSubview:_button1];
    
    
    self.button2 = [[UIButton alloc] initWithFrame:CGRectMake(15 + (aview.frame.size.width - 35) / 4  , 10, (aview.frame.size.width - 35) / 4, 30)];
    [_button2 setTag:11];
    [_button2 setTitle:@"目的地" forState:UIControlStateNormal];
    [_button2 setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    [_button2.titleLabel setFont:[UIFont systemFontOfSize:13]];
    _button2.layer.cornerRadius =10;
    _button2.layer.masksToBounds = YES;
    [_button2.layer setBorderWidth:1.0];
    [_button2.layer setBorderColor:[UIColor cyanColor].CGColor];//设置button边框颜色
    [_button2 addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [aview addSubview:_button2];
    
    
    self.button3 = [[UIButton alloc] initWithFrame:CGRectMake(20 + (aview.frame.size.width - 35) / 2  , 10, (aview.frame.size.width - 35) / 4, 30)];
    [_button3 setTag:12];
    [_button3 setTitle:@"出发城市" forState:UIControlStateNormal];
    [_button3 setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    [_button3.titleLabel setFont:[UIFont systemFontOfSize:13]];
    _button3.layer.cornerRadius =10;
    _button3.layer.masksToBounds = YES;
    [_button3.layer setBorderWidth:1.0];
    [_button3.layer setBorderColor:[UIColor cyanColor].CGColor];//设置button边框颜色
    [_button3 addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [aview addSubview:_button3];
    
    self.button4 = [[UIButton alloc] initWithFrame:CGRectMake(25 + (aview.frame.size.width - 35) / 4 * 3  , 10, (aview.frame.size.width - 35) / 4, 30)];
    [_button4 setTag:13];
    [_button4 setTitle:@"出发时间" forState:UIControlStateNormal];
    [_button4 setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
    [_button4.titleLabel setFont:[UIFont systemFontOfSize:13]];
    _button4.layer.cornerRadius =10;
    _button4.layer.masksToBounds = YES;
    [_button4.layer setBorderWidth:1.0];
    [_button4.layer setBorderColor:[UIColor cyanColor].CGColor];//设置button边框颜色
    [_button4 addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [aview addSubview:_button4];
    
    [_scroller addSubview:aview];
    [self.view addSubview:_scroller];
}


#pragma mark-
#pragma buttonTable 下拉列表
-(void)createButtonTable
{
    self.butTable = [[UITableView alloc] initWithFrame:CGRectMake(0, -(self.view.frame.size.height / 2), self.view.frame.size.width, self.view.frame.size.height / 2) style:UITableViewStylePlain];
    _butTable.delegate = self;
    _butTable.dataSource = self;
    [_scroller addSubview:_butTable];
    [_butTable registerClass:[UITableViewCell class ] forCellReuseIdentifier:@"nihao"];

}

-(void)createButtonTable1
{
    self.butTable1 = [[UITableView alloc] initWithFrame:CGRectMake(0, -(self.view.frame.size.height / 2), self.view.frame.size.width, self.view.frame.size.height / 2) style:UITableViewStylePlain];
    _butTable1.delegate = self;
    _butTable1.dataSource = self;
    [_scroller addSubview:_butTable1];
    [_butTable1 registerClass:[UITableViewCell class ] forCellReuseIdentifier:@"dajia"];
}

-(void)createButtonTable2
{
    self.butTable2 = [[UITableView alloc] initWithFrame:CGRectMake(0, -(self.view.frame.size.height / 2), self.view.frame.size.width, self.view.frame.size.height / 2) style:UITableViewStylePlain];
    _butTable2.delegate = self;
    _butTable2.dataSource = self;
    [_scroller addSubview:_butTable2];
    [_butTable2 registerClass:[UITableViewCell class ] forCellReuseIdentifier:@"meimei"];
}
-(void)createButtonTable3
{
    self.butTable3 = [[UITableView alloc] initWithFrame:CGRectMake(0, -(self.view.frame.size.height / 2), self.view.frame.size.width, self.view.frame.size.height / 2) style:UITableViewStylePlain];
    _butTable3.delegate = self;
    _butTable3.dataSource = self;
    [_scroller addSubview:_butTable3];
    [_butTable3 registerClass:[UITableViewCell class ] forCellReuseIdentifier:@"gege"];
}


#pragma mark -
#pragma clickButton

-(void)clickButton:(UIButton *)sender
{
    if (sender.tag == 10)
    {
        if (_butTable.frame.origin.y == - self.view.frame.size.height /2)
        {
        [UIView beginAnimations:@"ad" context:nil];
        [UIView setAnimationRepeatCount:1];
        [UIView setAnimationDuration:0.5];
        [_butTable setFrame:CGRectMake(0 , 40, self.view.frame.size.width,self.view.frame.size.height / 2)];
        [UIView commitAnimations];
        } else
        {
            [UIView beginAnimations:@"ad" context:nil];
            [UIView setAnimationRepeatCount:1];
            [UIView setAnimationDuration:0.5];
            [_butTable setFrame:CGRectMake(0 , - self.view.frame.size.height /2, self.view.frame.size.width,self.view.frame.size.height / 2)];
            [UIView commitAnimations];
            

        }
    } else if ( sender.tag == 11) {
        if (_butTable1.frame.origin.y == - self.view.frame.size.height /2)
        {
            [UIView beginAnimations:@"ad" context:nil];
            [UIView setAnimationRepeatCount:1];
            [UIView setAnimationDuration:0.5];
            [_butTable1 setFrame:CGRectMake(0 , 40, self.view.frame.size.width,self.view.frame.size.height / 2)];
            [UIView commitAnimations];
        } else
        {
            [UIView beginAnimations:@"ad" context:nil];
            [UIView setAnimationRepeatCount:1];
            [UIView setAnimationDuration:0.5];
            [_butTable1 setFrame:CGRectMake(0 , - self.view.frame.size.height /2, self.view.frame.size.width,self.view.frame.size.height / 2)];
            [UIView commitAnimations];
            
            
        }

    } else if ( sender.tag == 12) {
        if (_butTable2.frame.origin.y == - self.view.frame.size.height /2)
        {
            [UIView beginAnimations:@"ad" context:nil];
            [UIView setAnimationRepeatCount:1];
            [UIView setAnimationDuration:0.5];
            [_butTable2 setFrame:CGRectMake(0 , 40, self.view.frame.size.width,self.view.frame.size.height / 2)];
            [UIView commitAnimations];
        } else
        {
            [UIView beginAnimations:@"ad" context:nil];
            [UIView setAnimationRepeatCount:1];
            [UIView setAnimationDuration:0.5];
            [_butTable2 setFrame:CGRectMake(0 , - self.view.frame.size.height /2, self.view.frame.size.width,self.view.frame.size.height / 2)];
            [UIView commitAnimations];
            
            
        }
        
    } else if ( sender.tag == 13) {
        if (_butTable3.frame.origin.y == - self.view.frame.size.height /2)
        {
            [UIView beginAnimations:@"ad" context:nil];
            [UIView setAnimationRepeatCount:1];
            [UIView setAnimationDuration:0.5];
            [_butTable3 setFrame:CGRectMake(0 , 40, self.view.frame.size.width,self.view.frame.size.height / 2)];
            [UIView commitAnimations];
        } else
        {
            [UIView beginAnimations:@"ad" context:nil];
            [UIView setAnimationRepeatCount:1];
            [UIView setAnimationDuration:0.5];
            [_butTable3 setFrame:CGRectMake(0 , - self.view.frame.size.height /2, self.view.frame.size.width,self.view.frame.size.height / 2)];
            [UIView commitAnimations];
            
            
        }
        
    }

}




#pragma mark-
#pragma 下半部分的tableview
-(void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, _scroller.frame.size.width, _scroller.frame.size.height - 49 - 40) style:UITableViewStylePlain];
    _table.delegate = self;
    _table.dataSource = self;
    [_scroller addSubview:_table];
    [_table registerClass:[DIscountedTableViewCell class] forCellReuseIdentifier:@"ok"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _table) {
        return [_disArray count];
    } else if (tableView == _butTable) {
    return [_butArray count];
    } else if (tableView == _butTable1)
    {
        return [_butArray1 count];
    } else if (tableView == _butTable2)
    {
    return [_butArray2 count];
    } return [_butArray3 count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _table) {
        return self.view.frame.size.height / 2.5 ;

    }
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == _table) {
        DIscountedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok" forIndexPath:indexPath ];
        cell.selectionStyle = UITableViewCellAccessoryNone;
        DiscountedModel *model = [_disArray objectAtIndex:indexPath.row];
        [cell.image setImageWithURL:[NSURL URLWithString:model.cover]];
        
        [cell.lable1 setText:model.title];
        [cell.lable2 setText:model.depart_place];
        
        NSString *MinPrice = [NSString stringWithFormat:@"%ld", model.min_price];
        [cell.lable3 setText:MinPrice];
        [cell.lable3 setTextColor:[UIColor redColor]];
        return cell;

    }
        else if (tableView == _butTable)
    {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"nihao" forIndexPath:indexPath];
    cell.textLabel.text = [_butArray objectAtIndex:indexPath.row];
    
    return cell;
    } else if (tableView == _butTable1)
    {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dajia" forIndexPath:indexPath];
    cell.textLabel.text = [_butArray1 objectAtIndex:indexPath.row];
    return cell;
    } else if (tableView == _butTable2) {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"meimei" forIndexPath:indexPath];
    cell.textLabel.text = [_butArray2 objectAtIndex:indexPath.row];
    return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"gege" forIndexPath:indexPath];
    cell.textLabel.text = [_butArray3 objectAtIndex:indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _table)
    {
        DiscountedDetailsViewController *detail = [[DiscountedDetailsViewController alloc] init];
        DiscountedModel *model = [_disArray objectAtIndex:indexPath.row ];
        
        detail.receiveUrl = model.url;
        [self.navigationController pushViewController:detail animated:YES];

    } else if (tableView == _butTable)
    {
        
        [UIView beginAnimations:@"ad" context:nil];
        [UIView setAnimationRepeatCount:1];
        [UIView setAnimationDuration:1];
        [_butTable setFrame:CGRectMake(0 , - self.view.frame.size.height / 2, self.view.frame.size.width,self.view.frame.size.height / 2)];
        [UIView commitAnimations];
        [_button1 setTitle:[_butArray objectAtIndex:indexPath.row] forState:UIControlStateNormal];


    } else if (tableView == _butTable1)
    {
        [UIView beginAnimations:@"ad" context:nil];
        [UIView setAnimationRepeatCount:1];
        [UIView setAnimationDuration:1];
        [_butTable1 setFrame:CGRectMake(0 , - self.view.frame.size.height / 2, self.view.frame.size.width,self.view.frame.size.height / 2)];
        [UIView commitAnimations];
        [_button2 setTitle:[_butArray1 objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    } else if (tableView == _butTable2)
    {
        [UIView beginAnimations:@"ad" context:nil];
        [UIView setAnimationRepeatCount:1];
        [UIView setAnimationDuration:1];
        [_butTable2 setFrame:CGRectMake(0 , - self.view.frame.size.height / 2, self.view.frame.size.width,self.view.frame.size.height / 2)];
        [UIView commitAnimations];
        [_button3 setTitle:[_butArray2 objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    } else if (tableView == _butTable3)
    {
        [UIView beginAnimations:@"ad" context:nil];
        [UIView setAnimationRepeatCount:1];
        [UIView setAnimationDuration:1];
        [_butTable3 setFrame:CGRectMake(0 , - self.view.frame.size.height / 2, self.view.frame.size.width,self.view.frame.size.height / 2)];
        [UIView commitAnimations];
        [_button4 setTitle:[_butArray3 objectAtIndex:indexPath.row] forState:UIControlStateNormal];
    }


    
    
    
}




#pragma mark-
#pragma 下拉刷新上拉加载
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.table addHeaderWithTarget:self action:@selector(headerRereshing)];
    //#warning 自动刷新(一进入程序就下拉刷新)
    [self.table headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.table addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.table.headerPullToRefreshText = @"刷新";
//    self.table.headerReleaseToRefreshText = @"松开马上刷新了";
    self.table.headerRefreshingText = @"正在刷新";
    
     self.table.footerPullToRefreshText = @"加载";
//    self.table.footerReleaseToRefreshText = @"松开马上加载更多数据";
    self.table.footerRefreshingText = @"玩命加载中";
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        [self.table reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.table headerEndRefreshing];
    });
}

- (void)footerRereshing
{

    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"" forKey:@"category"];
    [dic setObject:@"" forKey:@"dest"];
    [dic setObject:@"" forKey:@"start"];
    [dic setObject:@"" forKey:@"depart"];

    NSString *pageStr = [NSString stringWithFormat:@"%d", _page];
    [dic setObject:pageStr forKey:@"page"];
    [AsyncConnection getConnectWithUrl:Detail value:dic block:^(NSData *data) {
        _myData = data;
        NSDictionary *dice = [NSJSONSerialization JSONObjectWithData:_myData options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [dice objectForKey:@"products"];
        for (NSDictionary *dice1 in arr) {
            DiscountedModel *disModel = [[DiscountedModel alloc] init];
            [disModel setValuesForKeysWithDictionary:dice1];
            [_disArray addObject:disModel];
        }
    }];
  
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.table reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.table footerEndRefreshing];
    });
    _page++;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
